package ch.bfh.mobicomp.soda2.wifi.ui;

import ch.bfh.mobicomp.soda2.wifi.R;
import ch.bfh.mobicomp.soda2.wifi.R.layout;
import ch.bfh.mobicomp.soda2.wifi.R.menu;
import ch.bfh.mobicomp.soda2.wifi.service.WifiService;
import ch.bfh.mobicomp.soda2.wifi.utils.WifiApManager;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class WifiMainActivity extends Activity {

	private static final String TAG = WifiService.class.getName();
	
	private TextView textViewConfig;
	private Button buttonToggleAp;
	private Button buttonToggleWifi;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wifi_main);
		
		this.textViewConfig = (TextView)findViewById(R.id.text_ap_activity_main_config);
		this.buttonToggleAp = (Button)findViewById(R.id.button_ap_activity_main_toggle_ap);
		this.buttonToggleWifi = (Button)findViewById(R.id.button_ap_activity_main_toggle_wifi);
		
		Intent intent = WifiService.getWifiServiceStartIntent();
		sendBroadcast(intent);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
		SharedPreferences settings = getSharedPreferences(WifiService.PREFS_NAME, 0);
		OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
			  public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
			    WifiMainActivity.this.refreshUI(prefs);
			  }
		};
		settings.registerOnSharedPreferenceChangeListener(listener);
		
		SharedPreferences prefs = getSharedPreferences(WifiService.PREFS_NAME, 0);
		this.refreshUI(prefs);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.wifi_main, menu);
		return true;
	}
	
	@Override
	protected void onDestroy() {
	    super.onDestroy();
	}
	
	public void onButtonClick(View view) {
		Log.d(TAG, "Button clicked from " + view.getId());
		
		switch (view.getId()) {
        	case R.id.button_ap_activity_main_toggle_ap:
        		Log.d(TAG, "Button toggle ap clicked");
        		this.toggleAccessPoint();
        		break;
        		
        	case R.id.button_ap_activity_main_toggle_wifi:
        		Log.d(TAG, "Button toggle wifi clicked");
        		//do some thing...
        		break;
        		
        	case R.id.button_ap_activity_main_refresh_clients:
        		Log.d(TAG, "Button refresh clients clicked");
        		this.refreshCleintList();
        		break;
		}
	}
	
	private void toggleAccessPoint() {
		SharedPreferences prefs = getSharedPreferences(WifiService.PREFS_NAME, 0);
		Intent intent = WifiService.getWifiApOnOffIntent(!prefs.getBoolean("AP_ENABLED", false));
		sendBroadcast(intent);	
	}
	
	private void refreshCleintList() {
		Intent intent = WifiService.getRefreshClientListIntent();
		sendBroadcast(intent);	
	}	
	
	private void refreshUI(SharedPreferences prefs) {
		this.setApButton(prefs.getBoolean("AP_ENABLED", false));				
		this.textViewConfig.setText("");
		this.textViewConfig.append("AP Config \n");
		this.textViewConfig.append("Enabled: " + prefs.getBoolean("AP_ENABLED", false) + "\n");
		this.textViewConfig.append("SSID: " + prefs.getString("AP_SSID", "unknown") + "\n");
		this.textViewConfig.append("Hidden SSID: " + prefs.getBoolean("AP_HIDDENSSID", false) + "\n");
		this.textViewConfig.append("Clients: " + prefs.getString("AP_CLIENTS", "unknown") + "\n");
	}
	
	private void setApButton(Boolean enabled) {
		if(enabled)
			this.buttonToggleAp.setText(R.string.button_activity_wifi_main_toggle_ap_off);
		else
			this.buttonToggleAp.setText(R.string.button_activity_wifi_main_toggle_ap_on);
	}
}